<?php
namespace app\api\controller\order;

use app\api\controller\AuthBase;
use app\common\business\Order as orderBusi;
use app\common\lib\Show;
// 订单API
class Index extends AuthBase
{
	//创建订单
	public function save(){
		if(!$this->request->isPost()){
			return Show::error();
		}
		$addressId = input('param.address_id',0,'intval');
		$ids =       input('param.ids','','trim');
		if(!$addressId || !$ids){
			return Show::error([],'参数错误');
		}
		$data = [
			'address_id'=>$addressId,
			'ids'=>$ids,
			'user_id'=>$this->userId,
		];
		
		try{
			$result = (new orderBusi())->save($data);
		}catch(\Exception $e){
			return Show::error([],$e->getMessage());
		}
		
		if(!$result){
			return Show::error([],'提交订单失败'); 
		} 
		return Show::success($result);
	}

	//获取订单详情
	public function read(){
		$id = input('param.id','','trim');
		if(empty($id)){
			return Show::error([],'参数错误');
		}
		$data = [
			'user_id' => $this->userId,
			'order_id' => $id,
		];
		
		$result = (new orderBusi())->detail($data);
		if(!$result){
			return Show::error([],'获取订单详情失败');
		}

		return Show::success($result);
	}
}